/* GUI CONVERTED from msmsiqa.p (converter v1.76) Wed Sep  4 17:08:18 2002 */
/* msmsiqa.p - MPS SUMMARY INQUIRY SUBROUTINE                           */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/* $Revision: 1.6.1.8 $                                           */

/*V8:ConvertMode=Report                                        */
/* REVISION: 1.0    LAST MODIFIED: 07/17/86     BY: EMB       */
/* REVISION: 1.0    LAST MODIFIED: 09/12/86     BY: EMB *20*  */
/* REVISION: 2.0    LAST MODIFIED: 03/06/87     BY: EMB *A39* */
/* REVISION: 4.0    LAST MODIFIED: 12/30/87     BY: WUG *4.0* */
/* REVISION: 4.0    LAST MODIFIED: 01/08/88     BY: EMB *A133**/
/* REVISION: 2.0    LAST MODIFIED: 01/06/88     BY: PML *A125**/
/* REVISION: 4.0    LAST MODIFIED: 12/30/87     BY: WUG *A137**/
/* REVISION: 2.0    LAST MODIFIED: 01/20/88     BY: PML *A158**/
/* REVISION: 4.0    LAST MODIFIED: 03/21/88     BY: EMB *A162**/
/* REVISION: 5.0    LAST MODIFIED: 12/20/88     BY: EMB *B005**/
/* REVISION: 5.0    LAST MODIFIED: 01/27/89     BY: EMB *A615**/
/* REVISION: 5.0    LAST MODIFIED: 11/08/89     BY: EMB *B386**/
/* REVISION: 5.0    LAST MODIFIED: 02/14/90     BY: EMB *B568**/
/* REVISION: 6.0    LAST MODIFIED: 05/18/90     BY: RAM *D026**/
/* REVISION: 6.0    LAST MODIFIED: 07/23/90     BY: emb *D040**/
/* REVISION: 6.0    LAST MODIFIED: 08/16/90     BY: emb *D057**/
/* REVISION: 6.0    LAST MODIFIED: 05/07/91     BY: emb *D618**/
/* REVISION: 7.0    LAST MODIFIED: 07/29/92     BY: emb *F816**/
/* REVISION: 7.2    LAST MODIFIED: 11/20/92     BY: jcd *G345**/
/* REVISION: 7.2    LAST MODIFIED: 09/01/94     BY: ljm *FQ67**/
/* REVISION: 8.6    LAST MODIFIED: 10/13/97     BY: ays *K0XF**/
/* REVISION: 8.6E   LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane */
/* REVISION: 8.6E   LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan   */
/* REVISION: 9.1    LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane */
/* REVISION: 9.1    LAST MODIFIED: 08/08/00   BY: *N0J4* Mudit Mehta      */
/* Old ECO marker removed, but no ECO header exists *F024*               */
/* Old ECO marker removed, but no ECO header exists *F0PN*               */
/* Revision: 1.6.1.5     BY: Russ Witt     DATE: 09/21/01 ECO: *P01H*      */
/* $Revision: 1.6.1.8 $    BY: John Pison     DATE: 09/03/02 ECO: *P0HM*      */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/


/*GUI preprocessor directive settings */
&SCOPED-DEFINE PP_GUI_CONVERT_MODE REPORT

{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */

/* ********** Begin Translatable Strings Definitions ********* */

&SCOPED-DEFINE msmsiqa_p_3 "Start Date"
/* MaxLen: Comment: */

&SCOPED-DEFINE msmsiqa_p_5 "QOH"
/* MaxLen: Comment: */

&SCOPED-DEFINE msmsiqa_p_6 "Bucket"
/* MaxLen: Comment: */

&SCOPED-DEFINE msmsiqa_p_12 "MRP Req"
/* MaxLen: Comment: */

&SCOPED-DEFINE msmsiqa_p_14 "End Date"
/* MaxLen: Comment: */

&SCOPED-DEFINE msmsiqa_p_15 "Cumulative ATP"
/* MaxLen: Comment: */

/* ********** End Translatable Strings Definitions ********* */

{wbrp02.i}

define shared variable path1 like prd_path.
define variable path like prd_path.

define new shared variable fcssite like si_site.
define new shared variable fcspart like pt_part.

define shared variable frwrd like soc_fcst_fwd.
define shared variable bck like soc_fcst_bck.
define shared variable fcsduedate as date.

define new shared workfile fsworkfile no-undo
   field fsdate as date
   field fsqty as decimal.

define variable week as integer.

define shared variable start as date label {&msmsiqa_p_3}.
define shared variable ending as date label {&msmsiqa_p_14}.
define variable start1 as date.
define variable old_start as date.
define shared variable part like mrp_part.
define variable part1 like mrp_part.
define shared variable site like in_site.
/* Modified array variables from extent 8 to extent 16 */
define variable interval as integer extent 16 format "->>>>>>9".
define variable sdate as date extent 16.
define variable avail_promise as decimal extent 16 format "->>>>>>9".
define variable ords as decimal extent 16 format "->>>>>>9".
define variable qoh as decimal extent 16 format "->>>>>>9".
define variable season_build as decimal extent 16 format "->>>>>>9".
define variable season_req as decimal extent 16 format "->>>>>>9".
define variable cum_atp as decimal extent 16 format "->>>>>>9" no-undo.
define variable forecast like qoh.
define variable prod_forecast like qoh.
define variable salesord like qoh.
define variable req like qoh.
define variable recpts like qoh.
define variable workdays as integer extent 16 format ">>>>,>>9".
define variable due_date like mrp_due_date.

define variable holiday as integer.
define variable i as integer.
define variable ii as integer no-undo.
define variable j as integer.
define variable qty like rq_qty_per.
define variable wostart as integer.
define variable nonwdays as decimal.
define variable elapsedays as integer.
define variable wdays as integer.
define variable overlap as integer.
define variable wointerval as integer.
define variable offset like rq_lt_off.
define variable ltoff like rq_lt_off.
define variable ltoffperiod as integer.
define variable lead as integer.
define shared variable dwm as character format "!(1)" label {&msmsiqa_p_6}.
define shared variable idays as integer format ">>9" label "#".
define shared variable dsp-neg-atp as logical.
define shared variable reserved-inventory   like in_qty_oh.
define shared variable atp-sales-order      like sod_nbr.
define shared variable atp-sales-order-line like sod_line.
define variable monthend as integer.
define variable not_part as integer.
define variable num_intervals as integer initial 7 format "9".
define variable more like mfc_logical.
define variable qty_oh like in_qty_oh.

define variable dateArray as character extent 13 no-undo.
define variable labelArray as character format "x(14)" extent 8 no-undo.
define variable dataArray as decimal extent 56 format "->>>>>>9" no-undo.
define variable seasonDataArray as decimal extent 7 format "->>>>>>9" no-undo.
define variable past as character no-undo.
define variable seasonBuildLabel as character format "x(14)" no-undo.
define variable separator as character extent 7 no-undo.
define variable k as integer no-undo.
define variable seasonBuildAvail as logical no-undo.

define new shared workfile atpfile no-undo
   field atpdate like atp_date
   field atpqty  like atp_qty.

path = path1.

find pt_mstr no-lock where pt_part = part no-error.
find si_mstr no-lock where si_site = site no-error.

qty_oh = 0.

find ptp_det no-lock where ptp_part = pt_part
   and ptp_site = si_site no-error.
find in_mstr no-lock where in_part = pt_part
   and in_site = si_site no-error.
if available in_mstr then qty_oh = in_qty_oh.

/*  Add any customer reserved inventory to on hand balance */
qty_oh = qty_oh + reserved-inventory.

{fcsdate.i today fcsduedate week si_site}

fcsduedate = fcsduedate - 7 * bck.

FORM /*GUI*/ 
   pt_part        colon 14 pt_desc1 no-label
   pt_mrp         colon 69 label {&msmsiqa_p_12}

   si_site        colon 7
   pt_mfg_lead    colon 32
   pt_plan_ord    colon 50
   pt_pm_code     colon 69

   qty_oh         colon 7 label {&msmsiqa_p_5}
   pt_um          no-label
   pt_pur_lead    colon 32
   pt_ord_qty     colon 50
   pt_ord_min     colon 69 format ">>>,>>9"

   pt_ord_pol     colon 14
   pt_sfty_stk    colon 32
   pt_yield_pct   colon 50
   pt_ord_max     colon 69 format ">>>,>>9"

   pt_ord_per     colon 14
   pt_sfty_time   colon 32
   pt_timefence   colon 50
   pt_ord_mult    colon 69 format ">>>,>>9"

with STREAM-IO /*GUI*/  frame bb side-labels width 80 no-attr-space.

/* SET EXTERNAL LABELS */
setFrameLabels(frame bb:handle).

FORM /*GUI*/ 
   
 RECT-FRAME       AT ROW 1 COLUMN 1
 RECT-FRAME-LABEL AT ROW 1 COLUMN 3 NO-LABEL VIEW-AS TEXT SIZE-PIXELS 1 BY 1
 SKIP(.1)  /*GUI*/
 past at 18 dateArray[1 for 6]
  dateArray[7] at 18 dateArray[8 for 6] skip
  separator[1] at 18 separator[2 for 6]
   labelArray[1] at 2 dataArray[1 for 7] skip
   labelArray[2] at 2 dataArray[8 for 7] skip
   labelArray[3] at 2 dataArray[15 for 7] skip
   labelArray[4] at 2 dataArray[22 for 7] skip
   labelArray[5] at 2 dataArray[29 for 7] skip
   labelArray[6] at 2 dataArray[36 for 7] skip
   labelArray[7] at 2 dataArray[43 for 7] skip
   labelArray[8] at 2 dataArray[50 for 7] skip
   seasonBuildLabel at 2 seasonDataArray[1 for 7] skip
   skip(.4)
with frame b width 82 no-labels no-attr-space
/* overlay row if {gpiswrap.i} then 9 else 8 */
NO-BOX THREE-D /*GUI*/.

 DEFINE VARIABLE F-b-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame b = F-b-title.
 RECT-FRAME-LABEL:HIDDEN in frame b = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame b =
  FRAME b:HEIGHT-PIXELS - RECT-FRAME:Y in frame b - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME b = 80 - .5 . 	/* FRAME b:WIDTH-CHARS - .5. */

if available ptp_det then
display
   ptp_part @ pt_part
   si_site
   pt_um
   ptp_ord_pol @ pt_ord_pol
   ptp_ord_min @ pt_ord_min
   pt_desc1

   in_mrp when (available in_mstr) @ pt_mrp
   pt_mrp when (not available in_mstr)
   ptp_ord_per @ pt_ord_per
   ptp_ord_max @ pt_ord_max

   ptp_plan_ord @ pt_plan_ord
   ptp_ord_qty @ pt_ord_qty
   ptp_ord_mult @ pt_ord_mult
   qty_oh
   ptp_sfty_stk @ pt_sfty_stk
   ptp_mfg_lead @ pt_mfg_lead
   ptp_yld_pct @ pt_yield_pct
   ptp_pm_code @ pt_pm_code
   ptp_sfty_tme @ pt_sfty_time
   ptp_pur_lead @ pt_pur_lead
   ptp_timefnce @ pt_timefence
with frame bb STREAM-IO /*GUI*/ .
else

display
   pt_part
   si_site
   pt_um
   pt_ord_pol
   pt_ord_min
   pt_desc1

   in_mrp when (available in_mstr) @ pt_mrp
   pt_mrp when (not available in_mstr)
   pt_ord_per
   pt_ord_max

   pt_plan_ord
   pt_ord_qty
   pt_ord_mult

   qty_oh
   pt_sfty_stk
   pt_mfg_lead
   pt_yield_pct
   pt_pm_code
   pt_sfty_time
   pt_pur_lead
   pt_timefence
with frame bb STREAM-IO /*GUI*/ .

pause 0 before-hide.

{mfdel.i "fsworkfile"}
if can-find (first fcs_sum where fcs_part = pt_part
   and fcs_site = si_site)
   or can-find (first mrp_det where mrp_dataset = "fcs_sum"
   and mrp_part = pt_part) then do:
   fcspart = pt_part.
   fcssite = si_site.
   {gprun.i ""msfsre.p""}
end.

fcspart = pt_part.
fcssite = si_site.

{gprun.i ""msapre.p""
         "(input atp-sales-order,
           input atp-sales-order-line,
           input reserved-inventory)
          "}

assign
   past = getTermLabel("PAST",9)
   labelArray[1] = getTermLabel("PRODUCTION_FORECAST",14)
   labelArray[2] = getTermLabel("FORECAST",14)
   labelArray[3] = getTermLabel("SALES_ORDERS",14)
   labelArray[4] = getTermLabel("GROSS_REQUIREMENTS",14)
   labelArray[5] = getTermLabel("MASTER_SCHEDULE",14)
   labelArray[6] = getTermLabel("PROJECTED_QUANTITY_ON_HAND",14)
   labelArray[7] = getTermLabel("AVAILABLE_TO_PROMISE",14)
   labelArray[8] = getTermLabel("CUMULATIVE_ATP",14)
   seasonBuildLabel = getTermLabel("SEASONAL_BUILD",14)
   separator = "--------".

repeat:

   
/*GUI*/ {mfguichk.i } /*Replace mfrpchk*/


   /* INITIALIZE VARIABLES */
   assign
      workdays = 0
      num_intervals = 15.

   /* COMPUTE START DATES */
   sdate[1] = low_date.
   {mfcsdate.i}

   /* CALCULATE NUMBER OF NON WORKING DAYS IN EACH WEEK */
   /* {mfcwdays.i} */

   /* INITIALIZE VARIABLES */
   assign
      forecast      = 0
      prod_forecast = 0
      avail_promise = 0
      salesord      = 0
      req           = 0
      recpts        = 0
      qoh           = 0
      ords          = 0
      season_build  = 0
      season_req    = 0.

   assign
      dateArray[1] = string(sdate[2])
      dateArray[2] = string(sdate[3])
      dateArray[3] = string(sdate[4])
      dateArray[4] = string(sdate[5])
      dateArray[5] = string(sdate[6])
      dateArray[6] = string(sdate[7])
      dateArray[7] = string(sdate[2] - 1)
      dateArray[8] = string(sdate[3] - 1)
      dateArray[9] = string(sdate[4] - 1)
      dateArray[10] = string(sdate[5] - 1)
      dateArray[11] = string(sdate[6] - 1)
      dateArray[12] = string(sdate[7] - 1)
      dateArray[13] = string(sdate[8] - 1).

   display
      past dateArray[1 for 13]
      separator[1 for 7]
   with frame b STREAM-IO /*GUI*/ .

   /* CALCULATE REQUIREMENTS FOR EACH INTERVAL */
   hide message no-pause.

   more = no.
   for each mrp_det no-lock where mrp_part = pt_part
         and mrp_site = si_site:

      if lookup(mrp_type,"demand,supply,supplyf,supplyp") = 0 then next.
      if mrp_dataset = "fcs_sum" then next.

      /* BYPASS IF ORDER & LINE MATCHES INPUT PARMS   */
      /* THIS IS USED BY ATP ENFORCEMENT TO ELIMINATE */
      /* DOUBLE COUNTING WHEN ANALYZING ATP FOR       */
      /* EXISTING SALES ORDERS                        */
      if mrp_nbr = atp-sales-order
      and mrp_line begins (string(atp-sales-order-line))
      then next.

      due_date = mrp_due_date.
      if mrp_dataset begins "fcs_" then do:
         if mrp_due_date < fcsduedate then next.
      end.

      do i = num_intervals + 1 to 1 by -1:
         if due_date >= sdate[i] then do:
            if mrp_dataset = "sod_det"
               or mrp_dataset = "pb_sold"
               or mrp_dataset = "pbo_sold"
               then salesord[i] = salesord[i] + mrp_qty.
            else
         if mrp_dataset = "fc_det"
               then season_req[i] = season_req[i] + mrp_qty.
            else
         if mrp_dataset = "sob_det"
               or mrp_dataset = "wod_det"
               or mrp_dataset = "wo_scrap"
               then req[i] = req[i] + mrp_qty.
            else
         if mrp_dataset = "wo_mstr"
               or mrp_dataset = "pod_det"
               or mrp_dataset = "req_det"
               then recpts[i] = recpts[i] + mrp_qty.
            else
            if mrp_dataset = "fcs_sum"
               then forecast[i] = forecast[i] + mrp_qty.
            else
            if mrp_dataset = "pfc_det"
               then prod_forecast[i] = prod_forecast[i] + mrp_qty.

            else if mrp_type = "demand" then req[i] = req[i] + mrp_qty.
            else recpts[i] = recpts[i] + mrp_qty.

            if i = num_intervals + 1 then more = yes.
            if i > num_intervals - 8 then more = yes.
            leave.
         end.
      end.
   end.

   for each fsworkfile by fsdate:

      if fsdate >= sdate[num_intervals - 7] then do:
         more = yes.
         leave.
      end.
      do i = num_intervals + 1 to 1 by -1:
         if fsdate >= sdate[i] then do:
            forecast[i] = forecast[i] + fsqty.
            leave.
         end.
      end.
   end.

   for each atpfile by atpdate:
      if atpdate >= sdate[num_intervals - 7] then do:
         more = yes.
         leave.
      end.
      do i = num_intervals + 1 to 1 by -1:
         if atpdate >= sdate[i] then do:
            avail_promise[i] = avail_promise[i] + atpqty.
            leave.
         end.
      end.
   end.

   qoh[1] = qty_oh - salesord[1] - forecast[1] - prod_forecast[1]
   - req[1] + recpts[1].

   do i = 2 to num_intervals + 1:
      assign
         qoh[i] = qoh[i - 1] - salesord[i] - forecast[i]
         - prod_forecast[i] - req[i] + recpts[i].

   end.

   for each fc_det where fc_part = pt_part
         and fc_site = si_site
         no-lock by fc_part by fc_start:
      do i = num_intervals to 1 by -1:
         if fc_start < sdate[i + 1]
            then season_build[i] = fc_qty.
         if fc_start >= sdate[i + 1] then leave.
      end.
   end.

   /* if display-neg-atp = yes recalculate ATP now  */
   if dsp-neg-atp = yes then do:
      avail_promise[1] = qty_oh + recpts[1] - req[1] - salesord[1] - season_build[1].
      do ii = 2 to 7:
         avail_promise[ii] = recpts[ii] - req[ii] - salesord[ii] - (season_build[ii] - season_build[ii - 1]).
      end.
   end.

   /* now calculate cumulative ATP (running total) */
   assign
     cum_atp = 0
     cum_atp[1] = avail_promise[1].

   do ii = 2 to 7:
      cum_atp[ii] = cum_atp[ii - 1] + avail_promise[ii].
   end.

   seasonBuildAvail = can-find(first fc_det where fc_part = pt_part
                               and fc_site = si_site).
   do k = 1 to 7:
      assign
         dataArray[k] = prod_forecast[k]
         dataArray[k + 7] = forecast[k]
         dataArray[k + 14] = salesord[k]
         dataArray[k + 21] = req[k]
         dataArray[k + 28] = recpts[k]
         dataArray[k + 35] = qoh[k]
         dataArray[k + 42] = avail_promise[k]
         dataArray[k + 49] = cum_atp[k]
         seasonDataArray[k] = season_build[k].
   end.

   display
      labelArray[1 for 8] dataArray[1 for 56]
      seasonBuildLabel when(seasonBuildAvail)
      seasonDataArray[1 for 7] when(seasonBuildAvail)
   with frame b STREAM-IO /*GUI*/ .

   hide message no-pause.

   num_intervals = 7.

   {mfsumtrl.i}

   status default.
end.

hide frame b.
hide frame bb.
{wbrp04.i}
